<?php

/**
 *
 * @author Lisette
 * @creation 01-07-2012 16:55 
 */

/**
 * Secondary Call Function
 * @param type $model (DAO)
 * @param type $controller 
 * @param type $action
 * @param type $queryString
 * @param type $render
 * @return type 
 */
function performAction($model, $controller, $action, $queryString = null, $render = 0) {    
    $controllerName = ucfirst($controller) . 'Controller';
    //check if the model is not set and create controller with an empty model
    if(isset($model)){
    	$modelDAO = ucfirst($model) . 'DAO';
    	$dispatch = new $controllerName($modelDAO, $controller, $action);
    }else{
    	$dispatch = new $controllerName(null, $controller, $action);
    }
    $dispatch->render = $render;
    return call_user_func_array(array($dispatch, $action), $queryString);
}

/**
 * Main Call Function
 * @global type $url
 * @global type $default 
 * /index.php?controller=value&model=value&action=value&parameters=array() 
 */
function callHook() {    
    $queryString = array();
	//print_r($_GET);
	//die();
    $controller = $_POST['controller'];
    $model = $_POST['model'];
    $action = $_POST['action'];
    $parameters = $_POST['parameters'];
    
    $dispatch = new $controller($model, $controller, $action);

    if ((int) method_exists($controller, $action)) {
        call_user_func_array(array($dispatch, $action), $parameters);        
    } else {
        /* Error Generation Code Here */
    }
}


/** Autoload any classes that are required **/

function __autoload($className) {
	//echo "<br>".ROOT . DS . 'application' . DS . 'controller' . DS . $className . '.class.php';
	if (file_exists(ROOT . DS . 'application' . DS . 'controller' . DS . strtolower($className) . '.class.php')) {
		require_once(ROOT . DS . 'application' . DS . 'controller' . DS . $className . '.class.php');
	} else if (file_exists(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.class.php')) {
		require_once(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.class.php');
	} else {
		//Error Generation Code Here 
	}
	
}

callHook();
